package com.eax.ControlLayer;
import com.eax.ModelLayer.*;
import java.util.ArrayList;

public class DVDController {
	private DVDCollectionModel dvdCollection; // DVD Container.
	private CopyCollectionModel copyCollection;

	public DVDController() {
		// Initialize the models.
		dvdCollection = new DVDCollectionModel();
		copyCollection = new CopyCollectionModel();
	}

	public DVDModel findDVD(int id) {
		return dvdCollection.getDVDById(id);
	}

	public void createDVD(int id, String title, String artist, String releaseDate) {
		dvdCollection.addDVD(id, title, artist, releaseDate);
	}

	public void deleteDVD(int id) {
		DVDModel temp = dvdCollection.getDVDById(id);
		dvdCollection.removeDVD(temp);
		copyCollection.removeAllForDVD(id);
	}

	public void createCopy(int serialNumber, int dvdId, String dateOfPurchase, double purchasePrice, String status, Boolean available) {
		copyCollection.addCopy(serialNumber, dvdId, dateOfPurchase, purchasePrice, status, available);
	}

	public ArrayList<DVDModel> getAllDvds() {
		return dvdCollection.getAllDvds();
	}

	public ArrayList<CopyModel> getAllCopies() {
		return copyCollection.getAllCopies();
	}

	public int getHighestId(){
		return dvdCollection.getHighestId();
	}

	public int getHighestSerialNumber(){
		return copyCollection.getHighestSerialNumber();
	}

	public void rentCopy(int serialNumber){
		CopyModel c = copyCollection.getCopyBySerialNumber(serialNumber);
		c.setAvailable(false);
	}

	public void returnCopy(int serialNumber){
		CopyModel c = copyCollection.getCopyBySerialNumber(serialNumber);
		c.setAvailable(true);
	}

	public int getAvailableCopy(int dvdId){
		int serialNumber = 0;

		for(CopyModel c : copyCollection.getAllCopies()){
			if(c.getDvdId() == dvdId){
				serialNumber = c.getSerialNumber();
			}
		}

		return serialNumber;
	}

	public CopyModel getCopyBySerialNumber(int serialNumber){
		CopyModel r = null;

		for(CopyModel c : copyCollection.getAllCopies()){
			if(c.getSerialNumber() == serialNumber){
				r = c;
			}
		}

		return r;
	}
}
